package com.qujia.station.controller;

import com.alibaba.fastjson.JSONObject;
import com.qujia.common.constant.UserConstants;
import com.qujia.common.core.controller.BaseController;
import com.qujia.common.core.domain.AjaxResult;
import com.qujia.common.core.domain.entity.SysUser;
import com.qujia.common.core.page.PageDomain;
import com.qujia.common.core.page.TableDataInfo;
import com.qujia.common.core.page.TableSupport;
import com.qujia.common.exception.BaseException;
import com.qujia.common.utils.SecurityUtils;
import com.qujia.common.utils.poi.ExcelUtil;
import com.qujia.station.domain.InventoryBatch;
import com.qujia.station.domain.InventoryDetails;
import com.qujia.station.domain.Station;
import com.qujia.station.service.IStationService;
import com.qujia.station.service.InventoryBatchService;
import com.qujia.station.service.InventoryDetailsService;
import com.qujia.station.service.RepositoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/Inventory")
@Slf4j
@Api(value = "非油库存盘点")
public class InventoryBatchController extends BaseController {

    @Autowired
    private InventoryBatchService inventoryBatchService;

    @Autowired
    private InventoryDetailsService inventoryDetailsService;

    @Autowired
    private IStationService stationService;

    @Autowired
    private RepositoryService repositoryService;

    /**
     * 查询库存盘点
     * */
    @PostMapping("/batch/list")
    @ApiOperation(value = "查询库存盘点", notes = "查询库存盘点", httpMethod = "POST")
    public TableDataInfo selectBatchList(@RequestBody InventoryBatch inventoryBatch){
        if(Objects.isNull(inventoryBatch) || Objects.isNull(inventoryBatch.getPageSize())
                || Objects.isNull(inventoryBatch.getPageNum())){
            return failDataTable();
        }

        SysUser user = SecurityUtils.getLoginUser().getUser();
        if(Objects.isNull(user)){
            return failUserTable();
        }
        getStationList(inventoryBatch,user);
        log.info("查询库存盘点inventoryBatch请求参数:[{}]", JSONObject.toJSONString(inventoryBatch));
        PageDomain pageDomain = TableSupport.buildPageRequest();
        pageDomain.setPageNum(inventoryBatch.getPageNum());
        pageDomain.setPageSize(inventoryBatch.getPageSize());
        startPage(pageDomain);
        List<InventoryBatch> inventoryBatches = inventoryBatchService.selectInventoryBatchList(inventoryBatch);
        log.info("查询库存盘点inventoryBatch返回结果:[{}]", inventoryBatches.size());
        return getDataTable(inventoryBatches);
    }

    private void getStationList(InventoryBatch inventoryBatch,SysUser user){
        if(UserConstants.LEVEL_TWO.equals(user.getUserLevel()) && Objects.isNull(inventoryBatch.getStationId())){
            if(Objects.nonNull(inventoryBatch.getStationId())){
               return;
           }
            List<Station> stationList = stationService.getStationByEnterpriseId(user.getEnterpriseId());
            if(CollectionUtils.isEmpty(stationList)){
                throw new BaseException("油企不存在");
            }
            List<Long> stationIdList = new ArrayList<>();
            for (Station station : stationList) {
                stationIdList.add(station.getStationId());
            }
            inventoryBatch.setStationIdList(stationIdList);
        } else if(UserConstants.LEVEL_THREE.equals(user.getUserLevel())){
            inventoryBatch.setStationId(user.getStationId());
        }
    }


    /**
     * 新增库存盘点
     * */
    @PostMapping("/batch/save")
    @ApiOperation(value = "新增库存盘点", notes = "新增库存盘点", httpMethod = "POST")
    public AjaxResult saveBatch(@RequestBody InventoryBatch inventoryBatch){
        if(Objects.isNull(inventoryBatch) || CollectionUtils.isEmpty(inventoryBatch.getInventoryDetailsList())){
            return AjaxResult.error("请求参数异常");
        }
        if(Objects.isNull(inventoryBatch.getStationId())){
            return AjaxResult.error("请选择盘点仓库或门店");
        }

        Station station = stationService.selectStationById(inventoryBatch.getStationId());
        inventoryBatch.setStationName(station.getStationName());

        log.info("新增库存盘点saveBatch请求参数:[{}]",JSONObject.toJSONString(inventoryBatch));
        long l = inventoryBatchService.saveInventoryBatch(inventoryBatch);
        log.info("新增库存盘点saveBatch返回结果:[{}]",l);
        return AjaxResult.success(l);
    }


    /**
     * 导出库存盘点
     * */
    @PostMapping("/batch/export")
    @ApiOperation(value = "导出库存盘点", notes = "导出库存盘点", httpMethod = "POST")
    public AjaxResult exportBatchList(@RequestBody InventoryBatch inventoryBatch){
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if(Objects.isNull(user)){
            return AjaxResult.error("用户不存在，请先登录");
        }
        getStationList(inventoryBatch,user);
        inventoryBatch.setPageSize(null);
        inventoryBatch.setPageNum(null);
        log.info("查询库存盘点inventoryBatch请求参数:[{}]", JSONObject.toJSONString(inventoryBatch));
        List<InventoryBatch> inventoryBatchList = inventoryBatchService.selectInventoryBatchList(inventoryBatch);
        log.info("查询库存盘点inventoryBatch返回结果:[{}]", inventoryBatchList.size());
        ExcelUtil<InventoryBatch> inventoryBatchExcelUtil = new ExcelUtil<>(InventoryBatch.class);
        return inventoryBatchExcelUtil.exportExcel(inventoryBatchList,"库存盘点");
    }


    /**
     * 查询盘点商品详情列表
     * */
    @GetMapping("/details/list")
    @ApiOperation(value = "查询盘点商品详情列表", notes = "查询盘点商品详情列表", httpMethod = "GET")
    public AjaxResult selectDetailsList(@RequestParam("batchId") Long batchId){
        if(Objects.isNull(batchId)){
            return AjaxResult.error("请求参数异常");
        }
        log.info("查询盘点商品详情列表selectDetailsList请求参数:[{}]", batchId);
        List<InventoryDetails> inventoryDetailsList = inventoryDetailsService.selectInventoryDetailsList(batchId);
        InventoryDetails inventoryDetails = inventoryDetailsService.selectInventoryDetailsSum(batchId);
        inventoryDetailsList.add(inventoryDetails);
        log.info("查询盘点商品详情列表selectDetailsList返回结果:[{}]", inventoryDetailsList.size());
        return AjaxResult.success(inventoryDetailsList);
    }
}
